//
// Created by root on 10/7/15.
//
#include <stdio.h>
#include <stddef.h>
#include "socket_thread_map.h"

void add_socket_thread(socket_thread_map ** map,pthread_t pthreadT, int socketid){
    socket_thread_map * mapNew;
    char pThreadTStr[50];
    snprintf(pThreadTStr,50,"%ld",pthreadT);
    printf("map add [%s => %d]\n",pThreadTStr,socketid);

    HASH_FIND_STR(*map,pThreadTStr,mapNew);
    if(mapNew == NULL){
        mapNew = (socket_thread_map*)malloc(sizeof(socket_thread_map));
        strcpy(mapNew->threadid,pThreadTStr);
        HASH_ADD_STR(*map,threadid,mapNew);
    }
    snprintf(mapNew->socketid,50,"%d",socketid);

}

void delete_socket_thread(socket_thread_map ** map,pthread_t pthreadT){
    socket_thread_map * mapDel;
    char pThreadTStr[50];
    snprintf(pThreadTStr,50,"%ld",pthreadT);
    HASH_FIND_STR(*map,pThreadTStr,mapDel);
    if(mapDel != NULL){
        HASH_DEL(*map,mapDel);
        free(mapDel);
    }
}

socket_thread_map * find_socket_thread(socket_thread_map ** map,pthread_t pthreadT){
    socket_thread_map * mapDel;
    char pThreadTStr[50];
    snprintf(pThreadTStr,50,"%ld",pthreadT);
    printf("map find [%s]\n",pThreadTStr);

    HASH_FIND_STR(*map,pThreadTStr,mapDel);
    return mapDel;
}